package com.foo.client;

import java.util.Properties;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;

public class SendToQueue {
	Queue queue = null;
	QueueConnectionFactory factory = null;
	QueueConnection connection = null;
	QueueSender sender = null;
	QueueSession session = null;
	
	public void sendMessage(String msg) {
		try {
			Properties props = new Properties();
			props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			props.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(props);
			factory = (QueueConnectionFactory) ctx.lookup("XAConnectionFactory");
			connection = factory.createQueueConnection();
			session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
			queue = (Queue) ctx.lookup("queue/HistoricalDataStorage");
			sender = session.createSender(queue);
			//create and set a message to send
			TextMessage message = session.createTextMessage(msg);


			sender.send(message);
			System.out.println("Sending message");   
			session.close ();
			connection.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
